/**
 * @Author: LAMMU
 * @FilePath: /nuxt3-template/Users/lammu/Desktop/workplace-vite/template/src/main.js
 * @Date: 2023-05-14 22:34:56
 * @LastEditors: LAMMU
 * @LastEditTime: 2023-05-15 00:12:13
 * @Description: 请添加文件描述
 */
import { createApp } from "vue";
import "./style.css";
import App from "./App.vue";
import { createRouter, createWebHistory, createWebHashHistory } from "vue-router";
import { microAppProps } from './microapp';

let app = null;

window.mount = () => {
  const router = createRouter({
    history: createWebHistory(window.__MICRO_APP_BASE_ROUTE__ || "/microappB/"),
    routes: [
      {
        path: "/empty",
        name: "empty",
        component: () => import("./components/empty.vue"),
        meta: { hidden: true, title: "中专路由" },
      },
      {
        path: "/a",
        name: "a",
        component: () => import("./components/A.vue"),
        meta: { hidden: true, title: "a" },
      },
      {
        path: "/b",
        name: "b",
        component: () => import("./components/B.vue"),
        meta: { hidden: true, title: "b" },
      },
      {
        path: "/c",
        name: "c",
        component: () => import("./components/C.vue"),
        meta: { hidden: true, title: "c" },
      },
    ],
  });
  app = createApp(App);
  app.use(router).mount("#appB");

  if (window.__MICRO_APP_ENVIRONMENT__) {
    window.microApp.addDataListener((data) => {
      microAppProps.value = JSON.parse(JSON.stringify(data));
      console.log('>> 子应用B: props', JSON.parse(JSON.stringify(data)));
    }, true)
  }
}

window.unmount = () => {
  app?.unmount();
  window.microApp.clearDataListener();
}

if (!window.__MICRO_APP_ENVIRONMENT__) window.mount();

console.log('子应用appB的window:', window);
console.log('子应用appB的window.__MICRO_APP_ENVIRONMENT__:', window.__MICRO_APP_ENVIRONMENT__, '判断应用是否在微前端环境中');
console.log('子应用appB的window.__MICRO_APP_NAME__:', window.__MICRO_APP_NAME__, '应用名称');
console.log('子应用appB的window.__MICRO_APP_PUBLIC_PATH__:', window.__MICRO_APP_PUBLIC_PATH__, '子应用的静态资源前缀');
console.log('子应用appB的window.__MICRO_APP_BASE_ROUTE__:', window.__MICRO_APP_BASE_ROUTE__, '子应用的基础路由');
console.log('子应用appB的window.__MICRO_APP_BASE_APPLICATION__:', window.__MICRO_APP_BASE_APPLICATION__, '判断应用是否是主应用');